package com.jeez.gateway.filter;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;

//@Component
public class AccessFilter extends ZuulFilter {

	private static final Logger LOG = LoggerFactory.getLogger(AccessFilter.class);  
	
	@Override
	public Object run() {
		RequestContext ctx = RequestContext.getCurrentContext();  
        HttpServletRequest request = ctx.getRequest();
        
        LOG.info("send {} request to {}", request.getMethod(), request.getRequestURL().toString());  
        
        //获取传来的参数accessToken  
        Object accessToken = request.getParameter("accessToken");  
        if(accessToken == null) {
            LOG.warn("access token is empty");
            //过滤该请求，不往下级服务去转发请求，到此结束
            ctx.setSendZuulResponse(false);  
            ctx.setResponseStatusCode(401);
            //ctx.setResponseBody("{\"result\":\"accessToken is empty!\"}");  
            return null;
        }
        //如果有token，则进行路由转发
        LOG.info("access token ok");  
        //这里return的值没有意义，zuul框架没有使用该返回值  
        return null;  
	}

	@Override
	public boolean shouldFilter() {
		//是否执行该过滤器，true代表需要过滤 
		return true;
	}

	@Override
	public int filterOrder() {
		//优先级. 数字越大，优先级越低  
		return 0;
	}

	/**
	 * post 后置过滤器
	 * pre 前置过滤器
	 * error 异常过滤器
	 */
	@Override
	public String filterType() {
		//前置过滤器
		return "pre";
	}

}
